package com.solocredit.finance.http

import com.solocredit.finance.app.MyApp
import com.solocredit.finance.base.utils.log.JsonFormat
import com.solocredit.finance.base.utils.log.MullaLog
import com.solocredit.finance.config.MullaConfigs
import com.solocredit.finance.http.ApiService.addCommonParams
import com.solocredit.finance.http.ApiService.addRandomParams
import com.solocredit.finance.http.HttpRequestBuilder.Callback
import com.solocredit.finance.utils.event.MullaBehaviorTracker
import com.solocredit.finance.utils.okhttp.callback.StringCallback
import com.solocredit.finance.utils.okhttp.model.HttpParams
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2025/3/24 17:55
 * @Annotation：
 */
class HttpConnectLoaderV2: IHttpLoader {

    override fun getMullaAction(url: String, params: HttpParams, callback: IHttpCallback) {
        MullaLog.e("HttpUrl：  $url")
        MullaLog.e("HttpParams： $params")
        HttpRequestBuilder()
            .url(url)
            .method("GET")
            .addHeader("product", MullaConfigs.MULLA_APPNAME)
            .addHeader("appid", MullaConfigs.MULLA_APPNAME)
            .timeouts(20000, 20000)
            .execute(object : Callback{
                override fun onSuccess(response: String?) {
                    if (response.isNullOrEmpty()){
                        val errorObj = JSONObject()
                        errorObj.put("errorCode", 999)
                        errorObj.put("errorMsg", "http request error")
                        callback.onRequestError(errorObj)
                    } else {
                        Thread {
                            MullaLog.i("url：$url --> " + JsonFormat.format(response))
                        }.start()
                        val data = JSONObject(response)
                        callback.onRequestSuccess(data)
                    }
                }

                override fun onFailure(code: Int, msg: String?) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode", code)
                    errorObj.put("errorMsg", msg)
                    callback.onRequestError(errorObj)
                }

                override fun onFinished() {
                    callback.onRequestFinish()
                }
            });
    }

    override fun postMullaAction(url: String, params: JSONObject, callback: IHttpCallback) {
        MullaLog.e("HttpUrl：  $url")
        MullaLog.e("HttpParams： $params")
        HttpRequestBuilder()
            .url(url)
            .method("POST")
            .addHeader("product", MullaConfigs.MULLA_APPNAME)
            .addHeader("appid", MullaConfigs.MULLA_APPNAME)
            .body(params.toString())
            .timeouts(20000, 20000)
            .execute(object : Callback {
                override fun onSuccess(response: String?) {
                    if (response.isNullOrEmpty()){
                        val errorObj = JSONObject()
                        errorObj.put("errorCode", 999)
                        errorObj.put("errorMsg", "http request error")
                        callback.onRequestError(errorObj)
                    } else {
                        Thread {
                            MullaLog.i("url：$url --> " + JsonFormat.format(response))
                        }.start()
                        val data = JSONObject(response)
                        callback.onRequestSuccess(data)
                    }
                }

                override fun onFailure(code: Int, msg: String?) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode", code)
                    errorObj.put("errorMsg", msg)
                    callback.onRequestError(errorObj)
                }

                override fun onFinished() {
                    callback.onRequestFinish()
                }
            });
    }

    /**
     * 提交埋点
     */
    override fun postMullaEventAction(url: String, params: JSONObject, callback: IHttpCallback) {
        //添加公共参数和随机参数
        params.addCommonParams().addRandomParams()
        postMullaAction(url,params,callback)
    }

    /**
     * 添加埋点
     */
    override fun addMullaEventAction(eventName: String, remark: String) {
        val eventType = eventName.uppercase()//转大写
        MullaLog.e("track event add : $eventType,$remark")
        //只记录事件，批量埋点逻辑
        MullaBehaviorTracker.getInstance(MyApp.context).trackEvent(eventType,remark)
    }

    override fun uploadMullaFileAction(httpUrl: String, filePath: String, callback: StringCallback) {
    }

    override fun uploadMullaFileAction(httpUrl: String, filePath: String, callback: IHttpCallback) {
        HttpRequestBuilder()
            .url(httpUrl)
            .method("POST")
            .addHeader("product", MullaConfigs.MULLA_APPNAME)
            .addHeader("appid", MullaConfigs.MULLA_APPNAME)
            .timeouts(20000, 20000)
            .uploadFile(filePath,object : Callback {
                override fun onSuccess(response: String?) {
                    if (response.isNullOrEmpty()){
                        val errorObj = JSONObject()
                        errorObj.put("errorCode", 999)
                        errorObj.put("errorMsg", "http request error")
                        callback.onRequestError(errorObj)
                    } else {
                        Thread {
                            MullaLog.i("url：$httpUrl --> " + JsonFormat.format(response))
                        }.start()
                        val data = JSONObject(response)
                        callback.onRequestSuccess(data)
                    }
                }

                override fun onFailure(code: Int, msg: String?) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode", code)
                    errorObj.put("errorMsg", msg)
                    callback.onRequestError(errorObj)
                }

                override fun onFinished() {
                    callback.onRequestFinish()
                }
            });
    }
}